<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" th:href="@{/res/css/amazeui.min.css}"/>
    <link rel="stylesheet" th:href="@{/res/css/admin.css}"/>
    <script th:src="@{/js/jquery-1.11.3.min.js}"></script>
    <script th:src="@{/myplugs/js/plugs.js}"></script>
</head>

<body>
<div class="admin-content-body">
    <div class="am-cf am-padding am-padding-bottom-0">
        <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">我的订单</strong><small></small></div>
    </div>

    <hr>
    <div>
        <button class="am-btn am-btn-primary am-round findOrder" value="A" id="topay">待支付</button>
        <button class="am-btn am-btn-primary am-round findOrder" value="B">待发货</button>
        <button class="am-btn am-btn-primary am-round findOrder" value="C">已发货</button>
        <button class="am-btn am-btn-primary am-round findOrder" value="D">已完成</button>
        <button class="am-btn am-btn-primary am-round findOrder" value="E">已取消</button>
    </div>
    <div class="am-g">
        <div class="am-u-sm-12">
            <form class="am-form">
                <table class="am-table am-table-striped am-table-hover table-main">
                    <thead>
                    <tr>
                        <th class="table-id">ID</th>
                        <th class="table-ctime">创建时间</th>
                        <th class="table-tmoney">总金额</th>
                        <th class="table-stime">发货时间(kg)</th>
                        <th class="table-state">状态</th>
                        <th class="table-no">物流跟踪号</th>
                        <th class="table-set">操作</th>
                    </tr>
                    </thead>

                    <tbody id="mytbody">

                    </tbody>

                </table>
            </form>
        </div>
    </div>
</div>
</body>

<script>
    //从url得到uid
    var result = new Object();
    result = getParameter();
    var uid = result["uid"];


    $(function () {
        $('.findOrder').on('click', function () {
            var state = $(this).val();
            console.log(state);
            $.ajax({
                type: 'POST',
                url: '/order/orderList',
                dataType: 'json',
                data: {uid: uid, state: state},
                success: function (mydata) {
                    console.log(mydata);
                    showData(mydata);
                },
                error: function () {
                    alert("error");
                }
            });
        })

        $("#topay").click();
    });


    function showData(data) {
        var setName = "";
        if (data[0].state == "待支付")
            setName = "支付";
        else if (data[0].state == "待发货")
            setName = "待发货"
        else if (data[0].state == "已发货")
            setName = "查询物流"
        else if (data[0].state == "已完成")
            setName = "查询物流"
        else
            setName = "已取消"

        var str = "";
        for (var i = 0; i < data.length; i++) {
            str += "<tr class='low-" + i + "'><td>" + data[i].id + "</td><td hidden='hidden'>" + data[i].bvoId
                + "</td><td hidden='hidden'>" + data[i].mvoId + "</td><td>" + timeFormat(data[i].createTime) + "</td><td>"
                + data[i].totalMoney + "</td><td>"
                + timeFormat(data[i].sendTime) + "</td><td>" + data[i].state + "</td><td>" + data[i].sendNo + "</td><td>" +
                "<div class='am-btn-toolbar'> " +
                "<div class='am-btn-group am-btn-group-xs'> ";
            if (data[i].state == "待支付")
                str += "<button class='am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only' onclick='pay(" + i + ")'>" +
                    "<span class='am-icon-trash-o'></span>" + setName + "</button>";
            else if (data[i].state == "已发货")
                str += "<button class='am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only' onclick='searchSend(" + i + ")'>" +
                    "<span class='am-icon-trash-o'></span>" + setName + "</button>";
            else if (data[i].state == "已完成")
                str += "<button class='am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only' onclick='searchSend(" + i + ")'>" +
                    "<span class='am-icon-trash-o'></span>" + setName + "</button>";
            str += "<button class='am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only' onclick='sendsOrderId(" + i + ")'>" +
                "<span class='am-icon-trash-o'></span>查看</button>" + "</div>" + "</div>" + "</td></tr>";
        }
        $("#mytbody").html(str);
    };

    //查看订单商品
    function sendsOrderId(i) {
        var arr = $(".low-" + i).children('td');
        var orderId = arr[0].outerText;
        url = "/orderOfGoods?oid=" + orderId;
        window.location.href = url;
        window.event.returnValue = false;
    };

    //物流页面
    function searchSend(i) {
        url = "/sendNo";
        window.location.href = url;
        window.event.returnValue = false;
    };

    //支付
    function pay(i) {
        var arr = $(".low-" + i).children('td');
        var orderId = arr[0].outerText;
        var bvo_id = arr[1].outerText;
        var mvo_id = arr[2].outerText;
        var totalMoney = arr[4].outerText;
        url = "/pay?oid=" + orderId + "&bid=" + bvo_id + "&totalMoney=" + totalMoney + "&mid=" + mvo_id;
        window.location.href = url;
        window.event.returnValue = false;
    };

    //解析url传值
    function getParameter() {
        var url = location.search;//获取url?后面的内容
        var result = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                result[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return result;
    }

    //时间转换
    function timeFormat(time) {
        var d = new Date(time);

        var year = d.getFullYear();       //年
        var month = d.getMonth() + 1;     //月
        var day = d.getDate();            //日

        var hh = d.getHours();            //时
        var mm = d.getMinutes();          //分
        var ss = d.getSeconds();           //秒

        var clock = year + "/";

        if (month < 10)
            clock += "0";

        clock += month + "/";

        if (day < 10)
            clock += "0";

        clock += day + " ";

        if (hh < 10)
            clock += "0";

        clock += hh + ":";
        if (mm < 10) clock += '0';
        clock += mm + ":";

        if (ss < 10) clock += '0';
        clock += ss;
        return (clock);
    }
</script>
</html>